# Copyright 2025 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("//build_tools/bazel:build_defs.oss.bzl", "iree_runtime_cc_library", "iree_runtime_cc_test")

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

##----------------------------------------------------------------------------##
## Public API
##----------------------------------------------------------------------------##

iree_runtime_cc_library(
    name = "libhsa",
    srcs = [
        "libhsa.c",
    ],
    hdrs = [
        "libhsa.h",
    ],
    textual_hdrs = [
        "libhsa_tables.h",
    ],
    deps = [
        "//runtime/src/iree/base",
        "//runtime/src/iree/base/internal",
        "//runtime/src/iree/base/internal:dynamic_library",
        "//runtime/src/iree/base/internal:path",
        "@hsa_runtime_headers",
    ],
)

iree_runtime_cc_test(
    name = "libhsa_test",
    srcs = ["libhsa_test.cc"],
    group = "iree-hal-drivers-amdgpu-tests",
    tags = [
        "driver=amdgpu",
        "nodocker",
    ],
    deps = [
        ":libhsa",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_library(
    name = "topology",
    srcs = [
        "topology.c",
    ],
    hdrs = [
        "topology.h",
    ],
    deps = [
        ":libhsa",
        "//runtime/src/iree/base",
    ],
)

iree_runtime_cc_test(
    name = "topology_test",
    srcs = ["topology_test.cc"],
    group = "iree-hal-drivers-amdgpu-tests",
    tags = [
        "driver=amdgpu",
        "nodocker",
    ],
    deps = [
        ":libhsa",
        ":topology",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

##----------------------------------------------------------------------------##
## Internal Utilities
##----------------------------------------------------------------------------##

# TODO(benvanik): implement omitted files.
iree_runtime_cc_library(
    name = "util",
    srcs = [
        "block_pool.c",
        "device_library.c",
        "info.c",
        "kfd.c",
        "vmem.c",
    ],
    hdrs = [
        "affinity.h",
        "block_pool.h",
        "device_library.h",
        "error_callback.h",
        "info.h",
        "kfd.h",
        "vmem.h",
    ],
    deps = [
        ":libhsa",
        ":topology",
        "//runtime/src/iree/base",
        "//runtime/src/iree/base/internal",
        "//runtime/src/iree/base/internal:bitmap",
        "//runtime/src/iree/base/internal:synchronization",
        "//runtime/src/iree/base/internal:threading",
        "//runtime/src/iree/hal/drivers/amdgpu/device:binaries",
        "//runtime/src/iree/hal/drivers/amdgpu/device:headers",
    ],
)

iree_runtime_cc_test(
    name = "block_pool_test",
    srcs = ["block_pool_test.cc"],
    group = "iree-hal-drivers-amdgpu-tests",
    tags = [
        "driver=amdgpu",
        "nodocker",
    ],
    deps = [
        ":topology",
        ":util",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_test(
    name = "device_library_test",
    srcs = ["device_library_test.cc"],
    group = "iree-hal-drivers-amdgpu-tests",
    tags = [
        "driver=amdgpu",
        "nodocker",
    ],
    deps = [
        ":topology",
        ":util",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_test(
    name = "kfd_test",
    srcs = ["kfd_test.cc"],
    group = "iree-hal-drivers-amdgpu-tests",
    tags = [
        "driver=amdgpu",
        "nodocker",
    ],
    deps = [
        ":libhsa",
        ":topology",
        ":util",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)

iree_runtime_cc_test(
    name = "vmem_test",
    srcs = ["vmem_test.cc"],
    group = "iree-hal-drivers-amdgpu-tests",
    tags = [
        "driver=amdgpu",
        "nodocker",
    ],
    deps = [
        ":libhsa",
        ":topology",
        ":util",
        "//runtime/src/iree/base",
        "//runtime/src/iree/testing:gtest",
        "//runtime/src/iree/testing:gtest_main",
    ],
)
